﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using DDF.Entity;
using ARR.UI.CountryService;

namespace ARR.UI.Models
{
    public interface ICountryMasterModel : IModelBase
    {
        List<Country> Countries { get; set; }

        void DeleteCountries(List<Country> SelectedCountries);

        void Save();
    }

    public class CountryMasterModel : ModelBase, ICountryMasterModel
    {
        public CountryMasterModel()
        {
            Countries = new List<Country>();
            LoadCountries();
        }


        public List<Country> Countries { get; set; }

        
        public void DeleteCountries(List<Country> SelectedCountries)
        {
            //TODO: Definir si se pueden eliminar paises y cual seria su logica de borrado.
        }

        public void Save()
        {
            var toBeSaved = Countries.Where(c => c.GetEntityState() != EntityState.Original).ToList();

            if (toBeSaved.Any())
            {
                CountryServiceClient service = new CountryServiceClient();

                try
                {
                    service.SaveCountries(toBeSaved);
                    WorkspaceData.Instance.Country = service.GetById(WorkspaceData.Instance.Country.Id);

                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }

                LoadCountries();

                
            }
        }

        private void LoadCountries()
        {
            CountryServiceClient service = new CountryServiceClient();
            try
            {
                Countries = service.GetAll();
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }
    }


    public class CountryMasterModelMock : MockModelBase, ICountryMasterModel
    {

        public CountryMasterModelMock()
        {
            Countries = new List<Country>();
            LoadCountries();
        }


        public List<Country> Countries { get; set; }


        public void DeleteCountries(List<Country> SelectedCountries)
        {
            int[] ids = SelectedCountries.Select(c => c.Id).ToArray();

            foreach (int id in ids)
            {
                Country delete = Countries.SingleOrDefault(st => st.Id == id);

                if (delete != null)
                    Countries.Remove(delete);
            }
        }

        public void Save()
        {
            
        }

        private void LoadCountries()
        {
            Countries.Add(new Country { Id = 1, Code = "C1", Name = "Country1", CurrencyISOCode = "ARS", CurrencyName = "Argentine Peso", IsVATIncluded = true, TaxRate = 1.52M, Convention = "NACS" });
            Countries.Add(new Country { Id = 2, Code = "C2", Name = "Country2", CurrencyISOCode = "USD", CurrencyName = "USA Dolar", IsVATIncluded = true, TaxRate = 1.52M, Convention = "NACS" });
            Countries.Add(new Country { Id = 3, Code = "C3", Name = "Country3", CurrencyISOCode = "EUR", CurrencyName = "Euro", IsVATIncluded = true, TaxRate = 1.52M, Convention = "NACS" });
            Countries.Add(new Country { Id = 4, Code = "C4", Name = "Country4", CurrencyISOCode = "EUR", CurrencyName = "Euro", IsVATIncluded = true, TaxRate = 1.52M, Convention = "NACS" });

            foreach (Country country in Countries)
            {
                country.ChangeState(EntityState.Original);
            }
        }
    }
}
